package controllers

import (
	"fmt"
	log "github.com/jeanphorn/log4go"
	"os"
	"strings"
)

type HomeController struct {
	CommonController
}

func init()  {
	log.LoadConfiguration("./conf/example.json")
}

var GO_PATH string = os.Getenv("GOPATH")

func (this *HomeController) Getmain() {
	this.Data["Website"] = "beego.me"
	this.Data["Email"] = "astaxie@gmail.com"
	this.TplName = "main/main.html"
}

func (this *HomeController) Index() {
	this.Data["gopath"] = GO_PATH
	this.TplName = "main/main.html"
}

func (this *HomeController) ShowLibs() {
	this.Data["gopath"] = GO_PATH
	libpackages := GetGolibPackages(GO_PATH)
	if libpackages != nil {
		this.Data["libpackages"] = libpackages
	}
	this.TplName = "main/lib.html"
}

func (this *HomeController) DownGitData() {
	githubAdress := this.GetString("githubAdress")
	log.Info("下载github网络资源 %s 开始",githubAdress)
	downloadResut := ExcuteGoGet(githubAdress)
	this.Data["downloadResut"] = downloadResut
	data := map[string]interface{}{"githubAdress": githubAdress, "result": downloadResut}
	this.Data["json"] = data
	log.Info("下载github网络资源 %s 结束 结果:$v",githubAdress,downloadResut)
	this.ServeJSON()
}

func (this *HomeController) ShowPackages() {
	PackageLibs := []PackageModel{}

	address := this.GetString("address")
	keywords := this.GetString("keywords")
	address = strings.ReplaceAll(address, "\\", "/")
	fmt.Println("解析地址:" + address + ",关键字:" + keywords)
	//temp_data_list := [...]string{"000001","000002","000003","000004","000005","000006"}
	parseGoProject(address, keywords)
	libpackages := GetGolibPackages(GO_PATH)
	github := PackageClassify["github"]
	if libpackages != nil {
		this.Data["libpackages"] = libpackages
		for index, githubName := range github {
			// var s *Student = new(Student)
			var p = PackageModel{githubName, index, false}
			for _, existsLib := range libpackages {
				if strings.Contains(githubName, existsLib) {
					p.LibExists = true
					break;
				}
			}
			PackageLibs = append(PackageLibs, p)
		}
	} else {
		for index, githubName := range github {
			var p = PackageModel{githubName, index, false}
			PackageLibs = append(PackageLibs, p)
		}
	}
	this.Data["github"] = PackageLibs
	this.Data["other"] = PackageClassify["other"]
	this.Data["golib"] = PackageClassify["golib"]
	this.Data["address"] = address
	this.Data["gopath"] = GO_PATH
	this.Data["keywords"] = keywords
	this.TplName = "main/main.html"
}
